<?php
namespace app\common\service\Sms;

class SmsCode
{
    private $sign = "【FMI矿机】";

    private $url = "http://120.26.38.54:8000/interface/smssend.aspx";

    private $account = 'zjkcc';

    private $password = 'zj147369';

    public $mobile;
    public $code;

    public function __construct($mobile, $code)
    {
        $this->mobile = $mobile;
        $this->code = $code;
    }

    public function send()
    {
        cache("code",$this->code);
        // return true;

        // 网建短信
        $sms_url = 'http://utf8.api.smschinese.cn/?Uid=ll80117316&Key=fmid8cd98f00b204e980&smsMob='.$this->mobile.'&smsText=您的验证码是：'.$this->code .',该验证码五分钟内有效！';
        send_sms_api($sms_url);
        return true;


        // $content = sprintf("%s 您的验证码：%s，5分钟内有效", $this->sign, $this->code);
        // $bodys = sprintf("account=%s&password=%s&mobile=%s&content=%s", $this->account, $this->password, $this->mobile, $content);

        // $result = $this->request($bodys);

        // $arr = explode(',', $result);
        // if ($arr[0] > 0) {
        //     return true;
        // }

        // return false;

    }

    private function request($bodys)
    {
        $headers = array();
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type" . ":" . "application/x-www-form-urlencoded; charset=UTF-8");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_URL, $this->url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $result = curl_exec($curl);
        curl_close($curl);

        return $result;
    }
}